package itsm.isperp.module.service.app;

import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.app.AppDict;
import itsm.isperp.module.repository.app.AppDictMapper;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

/**
 * 系统字典服务
 * 
 * @author lizx
 * @date 2013-2-17
 * @version V1.0
 */
@Service
public class AppDictService extends BaseService<AppDict> {

	@Autowired
	protected AppDictMapper appDictMapper;

	@Override
	public AppDictMapper getRepository() {
		return this.appDictMapper;
	}

	public List<AppDict> findByType(String typeCode, String orderColumn) {
		return this.appDictMapper.findByType(typeCode, orderColumn);
	}

	/**
	 * 根据字典类型查询字典类表
	 * 
	 * @param typeCode
	 *            类型代码
	 * @return 字典数据列表
	 */
	public List<AppDict> findByType(String typeCode) {
		return this.appDictMapper.findByType(typeCode, "order_num");
	}

	public AppDict findByTypeAndNameValue(String typeCode, String value) {
		JqGridRequest req = new JqGridRequest();
		req.addFilterRule("type_code", JqGridFilterRuleOp.eq, typeCode);
		req.addFilterRule("name", JqGridFilterRuleOp.eq, value);
		req.isQueryOne(true);

		return this.findOneByRequest(req);
	}

	public AppDict findByTypeAndCodeValue(String typeCode, String value) {
		JqGridRequest req = new JqGridRequest();
		req.addFilterRule("type_code", JqGridFilterRuleOp.eq, typeCode);
		req.addFilterRule("code", JqGridFilterRuleOp.eq, value);
		req.isQueryOne(true);

		return this.findOneByRequest(req);
	}

	/**
	 * 根据字典类型得到字典排序第一的对象
	 * 
	 * @param typeCode
	 *            类型代码
	 * @return 字典数据
	 */
	public AppDict findFirstOneByType(String typeCode) {

		JqGridRequest req = new JqGridRequest();
		req.addFilterRule("type_code", JqGridFilterRuleOp.eq, typeCode);
		req.setSort("order_num", Direction.ASC);
		return this.findOneByRequest(req);
	}

	public AppDict findLastOneByType(String typeCode) {
		JqGridRequest req = new JqGridRequest();
		req.addFilterRule("type_code", JqGridFilterRuleOp.eq, typeCode);
		req.setSort("order_num", Direction.DESC);
		return this.findOneByRequest(req);
	}

	public AppDict findLastOneByFilter(String filters) {

		JqGridRequest req = new JqGridRequest();
		req.setFilters(filters);
		req.setSort("order_num", Direction.DESC);

		return this.findOneByRequest(req);
	}

	/**
	 * 查询所有字典类型
	 * 
	 * @return 字典列表
	 */
	public List<AppDict> findAllType() {
		return this.appDictMapper.findAllType();
	}

	/**
	 * 返回初始状态（排序第一的状态）,如果获取不到返回null值
	 * 
	 * @param typeCode
	 *            类型代码
	 * @return 名称
	 */
	public String getInitStatus(String typeCode) {
		// 默认初始状态：排序第一的状态
		AppDict appDict = findFirstOneByType(typeCode);
		return appDict == null ? null : appDict.getName();
	}

	public void deleteByTypeCode(String typeCode) {
		this.appDictMapper.deleteByType(typeCode);
	}

}
